#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size(), ret = 0;
        int left = 0, right = 0;
        int count_zero = 0;
        for (; right < n; right++)
        {
            if (nums[right] == 0)
                ++count_zero;

            // if (count_zero > k)
            // {
            //     count_zero = k;
            //     while (nums[left++] != 0);
            // }
            while (count_zero > k)
            {
                if (nums[left++] == 0)  count_zero--;
            }
            ret = max(ret, right - left + 1);
        }

        return ret;
    }
};